/**
  ******************************************************************************
  * @file    clock.c
  * @author  Iron
  * @date    2022-01-01
  * @version v1.0
  * @brief   clock c file
  */

/* Private includes ----------------------------------------------------------*/
#include "contiki.h"

/*---------------------------------------------------------------------------*/
/* Log configuration */
#include "sys/log.h"
#define LOG_MODULE  "CH579_CLOCK"
#define LOG_LEVEL   LOG_LEVEL_MAIN

/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static volatile uint32_t systick_ticks;

/* Private function prototypes -----------------------------------------------*/

/*---------------------------------------------------------------------------*/
void SysTick_Handler(void)
{
    systick_ticks++;

    rtimer_run_next();

    /*
     * Inform the etimer library that the system clock has changed and that an
     * etimer might have expired.
     */
    if (etimer_pending())
    {
        etimer_request_poll();
    }
}

/*---------------------------------------------------------------------------*/
void
clock_init(void)
{
    /* Systick */
    SysTick_Config(SystemCoreClock / CLOCK_SECOND); // CLOCK_SECOND: 32, 1000000
    NVIC_SetPriority(SysTick_IRQn, 0);
    NVIC_EnableIRQ(SysTick_IRQn);

    /* RTC */
#if( CLK_OSC32K )
    R8_SAFE_ACCESS_SIG   = 0x57;
    R8_SAFE_ACCESS_SIG   = 0xa8;
    R8_CK32K_CONFIG     &= ~(RB_CLK_OSC32K_XT | RB_CLK_XT32K_PON);
    R8_CK32K_CONFIG     |= RB_CLK_INT32K_PON;
    R8_SAFE_ACCESS_SIG   = 0;
    Calibration_LSI();
#else
    R8_SAFE_ACCESS_SIG   = 0x57;
    R8_SAFE_ACCESS_SIG   = 0xa8;
    R8_CK32K_CONFIG     |= RB_CLK_OSC32K_XT | RB_CLK_INT32K_PON | RB_CLK_XT32K_PON;
    R8_SAFE_ACCESS_SIG   = 0;
#endif

    Calibration_LSI();

//  TMOS_TimerInit( 0 );
}

/*---------------------------------------------------------------------------*/
clock_time_t
clock_time(void)
{
    return systick_ticks;
}

/*---------------------------------------------------------------------------*/
unsigned long
clock_seconds(void)
{
    uint32_t sec;
    UINT16  day, sec2, t32k;

    day = R32_RTC_CNT_DAY & 0x3FFF;
    sec2 = R16_RTC_CNT_2S;
    t32k = R16_RTC_CNT_32K;

    sec  = sec2 * 2 + ((t32k < 0x8000) ? 0 : 1);
    sec += day * 86400;

    return sec;
}

/*---------------------------------------------------------------------------*/
void
clock_set_seconds(unsigned long sec)
{
    //  RTC_InitTime( 2020, 1, 1, 0, 0, 0 );    //RTC时钟初始化当前时间
}

/*---------------------------------------------------------------------------*/
void
clock_wait(clock_time_t ticks)
{
    clock_time_t start_ticks;

    start_ticks = clock_time();

    do
    {
    }
    while (clock_time() - start_ticks < ticks);
}

/*---------------------------------------------------------------------------*/
// 1001228 / 1000000 = 1.001228 us
void
clock_delay_usec(uint16_t dt)
{
    while (dt--)
    {
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        __NOP();
    }
}




/**
  * @}
  */

/******************* (C)COPYRIGHT 2022 ***** END OF FILE *********************/
